import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/Login'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/Dashboard'),
        meta: { title: '首页', icon: 'el-icon-data-analysis' }
      }
    ]
  },

  // {
  //   path: '/exam',
  //   redirect: '/group/examList',
  //   component: Layout,
  //   meta: { title: '可展开', icon: 'el-icon-data-analysis' },
  //   children: [
  //     {
  //       path: '/examList',
  //       name: 'examList',
  //       component: () => import('@/views/ExamList'),
  //       meta: { title: '考试列表', icon: 'el-icon-data-analysis' }
  //     },
  //     {
  //       path: '/review',
  //       name: 'review',
  //       component: () => import('@/views/Review'),
  //       meta: { title: '错题回顾', icon: 'el-icon-data-analysis' }
  //     }
  //   ]
  // },

  {
    path: '/questions',
    redirect: '/questions/list',
    component: Layout,
    meta: { title: '题库', icon: 'el-icon-receiving' },
    children: [
      {
        path: '/questions/list',
        name: 'questionList',
        component: () => import('@/views/QuestionList'),
        meta: { title: '题目列表', icon: 'el-icon-document' }
      },
      {
        path: '/questions/add',
        name: 'addQuestion',
        component: () => import('@/views/QuestionAdd'),
        meta: { title: '新编题目', icon: 'el-icon-circle-plus-outline' }
      },
      {
        path: '/questions/edit',
        name: 'editQuestion',
        component: () => import('@/views/QuestionEdit'),
        meta: { title: '编辑题目', icon: 'el-icon-circle-plus-outline' },
        hidden: true
      }
    ]
  },

  {
    path: '/papers',
    redirect: '/papers/list',
    component: Layout,
    meta: { title: '试卷集', icon: 'el-icon-files' },
    children: [
      {
        path: '/papers/list',
        name: 'paperList',
        component: () => import('@/views/PaperList'),
        meta: { title: '试卷列表', icon: 'el-icon-document' }
      },
      {
        path: '/papers/add',
        name: 'addPapers',
        component: () => import('@/views/PaperEdit'),
        meta: { title: '新编试卷', icon: 'el-icon-circle-plus-outline' }
      },
      {
        path: '/papers/list/detail',
        name: 'paperDetail',
        hidden: true,
        component: () => import('@/layout/ExamLayout'),
        meta: { title: '试卷详情', icon: 'el-icon-circle-plus-outline' }
      }
    ]
  },

  // {
  //   path: '/papers',
  //   name: 'paperDetail',
  //   hidden: true,
  //   meta: { title: '试卷详情', icon: 'el-icon-document' }
  // },

  // {
  //   path: '/papers/list/detail',
  //   component: Layout,
  //   hidden:true,
  //   children: [
  //     {
  //       path: '',
  //       name: 'paperDetail',
  //       component: () => import('@/layout/ExamLayout'),
  //       meta: { title: '试卷详情', icon: 'el-icon-user' }
  //     }
  //   ]
  // },

  {
    path: '/exams',
    redirect: '/exams/list',
    component: Layout,
    meta: { title: '考试/练习', icon: 'el-icon-odometer' },
    children: [
      {
        path: '/exams/list',
        name: 'examList',
        component: () => import('@/views/ExamList'),
        meta: { title: '考试/练习列表', icon: 'el-icon-document' }
      },
      {
        path: '/exams/add',
        name: 'addExam',
        component: () => import('@/views/ExamEdit'),
        meta: { title: '发布考试/练习', icon: 'el-icon-circle-plus-outline' }
      }
    ]
  },

  {
    path: '/user',
    component: Layout,
    children: [
      {
        path: '',
        name: 'user',
        component: () => import('@/views/User'),
        meta: { title: '个人资料', icon: 'el-icon-user' }
      }
    ]
  },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () =>
  new Router({
    // mode: 'history', // require service support
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes
  })

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
